package com.simple.space.web.controller;

import com.simple.space.common.exception.BusinessException;
import com.simple.space.modules.exportexcel.pojo.Student;
import com.simple.space.modules.exportexcel.service.ExportExcelTestService;
import com.simple.space.util.excel.ExcelExportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xiongzhenchuan
 * @date 2020/11/30 15:24
 * @description
 */
@RestController
@RequestMapping("/export")
public class ExportExcelTestController {

    @Autowired
    private ExportExcelTestService exportExcelTestService;

    @GetMapping("/excel")
    public void exportStudent(HttpServletRequest request,
                                HttpServletResponse response,
                                @RequestParam(value = "headers")String headers,
                                @RequestParam(value = "fields")String fields){
        try {
            List<Student> stu = exportExcelTestService.exportStudent();

            ExcelExportUtil.excelExport(request,response,"学生信息",
                    headers.split(","),fields.split(","),stu);
        } catch (Exception e) {
            throw new BusinessException("导出学生Excel错误", e);
        }


    }

}
